//
//  ThumbnailGalleryView.m
//  NewApp
//
//  Created by  on 12-6-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ThumbnailGalleryView.h"
#import "NATextImageView.h"

@implementation ImgRect

@synthesize start;
@synthesize end;

@end


@implementation ThumbnailGalleryView
@synthesize imgsRect;
@synthesize imgsView;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
    }
    return self;
}


- (void) setGalleryWithLargeImages:(NSArray *) largeImages {
    
    if (largeImages == nil || largeImages.count == 0) 
        return;
    
    if (self.lastImageRect.size.width > 0) {
        return [self addGalleryimages:largeImages];
    }
    
    CGSize size = self.bounds.size;
    
    //Rows and cols in gallerys
    NSInteger cols = 3;
    NSInteger rows = (largeImages.count % cols == 0) ?  (largeImages.count / cols) : (largeImages.count / cols) + 1;
    
    CGFloat cellWidth   = size.width / 3;
    CGFloat cellHeight  = size.height / self.perferRows;
    NSInteger iconWidth = 95.f;
    NSInteger iconHeight= 80.f;
    CGFloat startX      = (cellWidth - iconWidth) / 2;
    CGFloat startY      = (cellHeight - iconHeight) / 2 - 7;
    
    self.contentSize = CGSizeMake(self.bounds.size.width, rows*cellHeight + 20.f);
    
    //add by liuxueyan
    if (imgsRect == NULL || [imgsRect count]<1) {
        imgsRect = [[NSMutableArray alloc] initWithCapacity:0];
    }
    
    if (imgsView == NULL || [imgsView count]<1) {
        imgsView = [[NSMutableArray alloc] initWithCapacity:0];
    }

    for (int i = 0; i < rows; i ++) {
        for (int k = 0; k < 3; k ++) {
            
            int index = i * 3 + k;
            if (index >= largeImages.count)
                break;
            
            CGFloat x = k * cellWidth + startX;
            CGFloat y = i * cellHeight+ startY;
            CGRect rect = CGRectMake(x, y, iconWidth, iconHeight);
            
            //add by liuxueyan
            ImgRect *imgRect = [[ImgRect alloc] init];
            imgRect.start = CGPointMake(x, y);
            imgRect.end = CGPointMake(x+iconWidth, y+iconHeight);
            [imgsRect addObject:imgRect];

            NAImage *image = [largeImages objectAtIndex:index];
            NATextImageView *imageView = [[NATextImageView alloc] initWithFrame:rect];
            [imageView setNaImage:image];
            [imageView addTarget:self action:@selector(imageViewDidPressed:) forControlEvents:UIControlEventTouchUpInside];

            [imgsView addObject:imageView]; //add by liuxueyan
            
            UIImage *img = [g_imageLoader cachedImageForURL:image.URL target:imageView];
            if (img != nil) {
                [imageView didLoadImage:image.URL Image:img];
            }
            [imageView setImageDescription:image.title];
            [self addSubview:imageView];
            self.lastImageRect = imageView.frame;
            [imageView release];
        }
    }
}

- (void) addGalleryimages:(NSArray *) galleryImages {
    
    if (galleryImages == nil || galleryImages.count == 0) 
        return;
    
    CGSize size = self.bounds.size;
    
    //Rows and cols in gallerys
    NSInteger cols = 3;
    NSInteger rows = (galleryImages.count % cols == 0) ?  (galleryImages.count / cols) : (galleryImages.count / cols) + 1;
    
    CGFloat cellWidth   = size.width / 3;
    CGFloat cellHeight  = size.height / self.perferRows;
    NSInteger iconWidth = 95.f;
    NSInteger iconHeight= 80.f;
    CGFloat startX      = (cellWidth - iconWidth) / 2;
    CGFloat startY      = (cellHeight - iconHeight) / 2 - 7 + self.lastImageRect.origin.y + iconHeight + 14.f;
    
    self.contentSize = CGSizeMake(self.bounds.size.width, self.contentSize.height + rows*cellHeight + 20.f);
    
    //add by liuxueyan
    if (imgsRect == NULL || [imgsRect count]<1) {
        imgsRect = [[NSMutableArray alloc] initWithCapacity:0];
    }
    
    if (imgsView == NULL || [imgsView count]<1) {
        imgsView = [[NSMutableArray alloc] initWithCapacity:0];
    }
    
    for (int i = 0; i < rows; i ++) {
        for (int k = 0; k < 3; k ++) {
            
            int index = i * 3 + k;
            if (index >= galleryImages.count)
                break;
            
            CGFloat x = k * cellWidth + startX;
            CGFloat y = i * cellHeight+ startY;
            CGRect rect = CGRectMake(x, y, iconWidth, iconHeight);
            
            //add by liuxueyan
            ImgRect *imgRect = [[ImgRect alloc] init];
            imgRect.start = CGPointMake(x, y);
            imgRect.end = CGPointMake(x+iconWidth, y+iconHeight);
            [imgsRect addObject:imgRect];
            
            NAImage *image = [galleryImages objectAtIndex:index];
            NATextImageView *imageView = [[NATextImageView alloc] initWithFrame:rect];
            [imageView setNaImage:image];
            [imageView addTarget:self action:@selector(imageViewDidPressed:) forControlEvents:UIControlEventTouchUpInside];
            
            [imgsView addObject:imageView]; //add by liuxueyan
            
            UIImage *img = [g_imageLoader cachedImageForURL:image.URL target:imageView];
            if (img != nil) {
                [imageView didLoadImage:image.URL Image:img];
            }
            [imageView setImageDescription:image.title];
            [self addSubview:imageView];
            self.lastImageRect = imageView.frame;
            [imageView release];
        }
    }

}

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	CGPoint offset = scrollView.contentOffset;       
    CGRect bounds = scrollView.bounds;       
    CGSize size = scrollView.contentSize;       
    UIEdgeInsets inset = scrollView.contentInset;       
    float y = offset.y + bounds.size.height - inset.bottom;       
    float h = size.height;       
	
    float reload_distance = 0;       
    if(y >= h + reload_distance) {           
		if (self.delegate != nil) {
            if ([self.delegate respondsToSelector:@selector(galleryWillLoadMoreImages)]) {
                [self.delegate galleryWillLoadMoreImages];
            }
        }
        
        NSLog(@"Should load more image");
    }   	
}




@end
